package com.harmony.pilot.knowledge.entity;

import lombok.Data;
import jakarta.persistence.*;
import java.time.LocalDateTime;

/**
 * 代码片段实体 - 用于RAG (检索增强生成)
 * 存储代码片段及其向量嵌入，供LLM使用
 */
@Data
@Entity
@Table(name = "code_snippet", indexes = {
    @Index(name = "idx_file_path", columnList = "file_path"),
    @Index(name = "idx_snippet_type", columnList = "snippet_type"),
    @Index(name = "idx_module", columnList = "module_name")
})
public class CodeSnippet {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    /** 文件路径 */
    @Column(name = "file_path", nullable = false, length = 1000)
    private String filePath;
    
    /** 相对路径 */
    @Column(name = "relative_path", length = 1000)
    private String relativePath;
    
    /** 所属模块 */
    @Column(name = "module_name", length = 255)
    private String moduleName;
    
    /** 子模块 */
    @Column(name = "sub_module", length = 255)
    private String subModule;
    
    /** 代码片段类型 (FILE_HEADER, CLASS, FUNCTION, TEST, MOCK, FUZZER, GN_BUILD, etc.) */
    @Column(name = "snippet_type", nullable = false, length = 50)
    private String snippetType;
    
    /** 代码片段内容 */
    @Column(name = "content", columnDefinition = "TEXT", nullable = false)
    private String content;
    
    /** 代码片段摘要 */
    @Column(name = "summary", columnDefinition = "TEXT")
    private String summary;
    
    /** 元数据 (JSON格式) - 包括函数名、类名、参数等 */
    @Column(name = "metadata", columnDefinition = "TEXT")
    private String metadata;
    
    /** 版权信息 */
    @Column(name = "copyright_info", columnDefinition = "TEXT")
    private String copyrightInfo;
    
    /** 许可证 */
    @Column(name = "license_info", length = 500)
    private String licenseInfo;
    
    /** 作者信息 */
    @Column(name = "author_info", columnDefinition = "TEXT")
    private String authorInfo;
    
    /** 开始行号 */
    @Column(name = "start_line")
    private Integer startLine;
    
    /** 结束行号 */
    @Column(name = "end_line")
    private Integer endLine;
    
    /** 代码行数 */
    @Column(name = "line_count")
    private Integer lineCount;
    
    /** Token数量 (用于LLM上下文计算) */
    @Column(name = "token_count")
    private Integer tokenCount;
    
    /** 向量嵌入 (存储为JSON数组字符串) */
    @Column(name = "embedding_vector", columnDefinition = "TEXT")
    private String embeddingVector;
    
    /** 向量维度 */
    @Column(name = "embedding_dimension")
    private Integer embeddingDimension;
    
    /** 嵌入模型名称 */
    @Column(name = "embedding_model", length = 100)
    private String embeddingModel;
    
    /** 是否是测试代码 */
    @Column(name = "is_test")
    private Boolean isTest = false;
    
    /** 是否是Mock代码 */
    @Column(name = "is_mock")
    private Boolean isMock = false;
    
    /** 是否是Fuzzer代码 */
    @Column(name = "is_fuzzer")
    private Boolean isFuzzer = false;
    
    /** 是否包含data/size参数 (fuzzer特征) */
    @Column(name = "has_data_size_params")
    private Boolean hasDataSizeParams = false;
    
    /** Include依赖 (JSON数组) */
    @Column(name = "includes", columnDefinition = "TEXT")
    private String includes;
    
    /** 关键词 (用于快速匹配) */
    @Column(name = "keywords", columnDefinition = "TEXT")
    private String keywords;
    
    /** 语言 (cpp, c, gn) */
    @Column(name = "language", length = 20)
    private String language;
    
    /** 文件MD5 */
    @Column(name = "file_md5", length = 32)
    private String fileMd5;
    
    /** 内容MD5 (用于去重) */
    @Column(name = "content_md5", length = 32)
    private String contentMd5;
    
    /** 相关代码片段ID列表 (JSON数组) */
    @Column(name = "related_snippets", columnDefinition = "TEXT")
    private String relatedSnippets;
    
    /** 索引时间 */
    @Column(name = "indexed_at")
    private LocalDateTime indexedAt;
    
    /** 创建时间 */
    @Column(name = "created_at", nullable = false, updatable = false)
    private LocalDateTime createdAt;
    
    /** 更新时间 */
    @Column(name = "updated_at")
    private LocalDateTime updatedAt;
    
    @PrePersist
    protected void onCreate() {
        createdAt = LocalDateTime.now();
        updatedAt = LocalDateTime.now();
        indexedAt = LocalDateTime.now();
    }
    
    @PreUpdate
    protected void onUpdate() {
        updatedAt = LocalDateTime.now();
    }
}

